<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/07/25 14:00:21 $)
// $RCSfile: staff_manage.php,v $ : $Revision: 1.20 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["t_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./includes/functions_html.php");
$grid = new Grid();
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("tickets_auditlogs");
require_once ("./modules/tickets/functions_ticketsui.php");

/**
* ###############################################
* MANAGE TICKETS
* ###############################################
*/
if ($eventaction == "manage")
{
	unset($_ticketsearchid); unset($noresult);
	$template->assign("ticketlabelid", intval($_REQUEST["ticketlabelid"]));
	
	if ($_GET["do"] == "dellabel" && !empty($_GET["ticketlabelid"]) && $_SWIFT["staff"]["tdellabel"] != 0)
	{
		require_once ("./modules/tickets/functions_labels.php");
		deleteTicketLabels(array($_GET["ticketlabelid"]));

		printRedirect($_SWIFT["language"]["ticketlabeldelconfirm"], "index.php?_m=tickets&_a=manage".iif(!empty($_REQUEST["departmentid"]), "&departmentid=". intval($_REQUEST["departmentid"])) .iif(!empty($_REQUEST["ticketstatusid"]), "&ticketstatusid=". intval($_REQUEST["ticketstatusid"])) .iif(!empty($_REQUEST["ticketsearchid"]), "&ticketsearchid=".intval($_REQUEST["ticketsearchid"])).iif(!empty($_REQUEST["ticketlabelid"]), "&ticketlabelid=".intval($_REQUEST["ticketlabelid"]))."&ldconfirm=1");
		exit;
	} else if ($_GET["do"] == "dellabel" && $_SWIFT["staff"]["tdellabel"] == 0) {
		$_SWIFT["errormessage"] = $_SWIFT["language"]["nopermission"];
	}

	// Check to see if mass action was carried out
	if (count($_POST["itemid"]))
	{
		require_once ("./modules/tickets/functions_ticketcore.php");
		$domassaction = false;
		if ($_POST["ma_staffid"] != "")
		{
			$_ma_staffid = intval($_POST["ma_staffid"]);
			$domassaction = true;
		} else {
			$_ma_staffid = false;
		}
		if (!empty($_POST["ma_departmentid"]))
		{
			$_ma_departmentid = intval($_POST["ma_departmentid"]);
			$domassaction = true;
		} else {
			$_ma_departmentid = false;
		}
		if (!empty($_POST["ma_ticketstatusid"]))
		{
			$_ma_ticketstatusid = intval($_POST["ma_ticketstatusid"]);
			$domassaction = true;
		} else {
			$_ma_ticketstatusid = false;
		}
		if (!empty($_POST["ma_priorityid"]))
		{
			$_ma_priorityid = intval($_POST["ma_priorityid"]);
			$domassaction = true;
		} else {
			$_ma_priorityid = false;
		}
		if (trim($_POST["ma_flagtype"]) != "")
		{
			$_ma_flagtype = $_POST["ma_flagtype"];
			$domassaction = true;
		} else {
			$_ma_flagtype = "-1";
		}
		if ($domassaction == true)
		{
			updateTicketStatus($_POST["itemid"], true, $_ma_staffid, $_ma_departmentid, $_ma_ticketstatusid, $_ma_priorityid, $_ma_flagtype);
		}

		if ($_POST["ma_options"] == "delete" && $_SWIFT["staff"]["tdeleteticket"] != "0")
		{
			deleteTickets($_POST["itemid"], true);
		} else if ($_POST["ma_options"] == "bandelete" && $_SWIFT["staff"]["tbandelete"] != "0") {
			banTickets($_POST["itemid"], true);
			deleteTickets($_POST["itemid"], true);
		} else if ($_POST["ma_options"] == "markasdue") {
			ticketsMarkAsDue($_POST["itemid"]);
		} else if ($_POST["ma_options"] == "markduein24") {
			ticketsMarkAsDue($_POST["itemid"], (DATENOW+86400));
		} else if ($_POST["ma_options"] == "mergetickets" && count($_POST["itemid"]) > 1) {
			// Ok, we need to merge the tickets
			require_once ("./modules/tickets/functions_ticketmain.php");

			$_ticketlist = $_POST["itemid"];
			unset($_ticketlist[0]);
			$parentticketid = $_POST["itemid"][0];
			mergeTickets($parentticketid, $_ticketlist);
		} else if ($_POST["ma_options"] == "print" && count($_POST["itemid"])) {
			massPrintTickets($_POST["itemid"]);
			exit;
		} else if ($_POST["ma_options"] == "reply" && count($_POST["itemid"])) {
			require_once ("./modules/tickets/staff_massreply.php");
			exit;
		}

		printRedirect($_SWIFT["language"]["massactionconfirm"], "index.php?_m=tickets&_a=manage".iif(!empty($_REQUEST["departmentid"]), "&departmentid=". intval($_REQUEST["departmentid"])) .iif(!empty($_REQUEST["ticketstatusid"]), "&ticketstatusid=". intval($_REQUEST["ticketstatusid"])) .iif(!empty($_REQUEST["ticketsearchid"]), "&ticketsearchid=".intval($_REQUEST["ticketsearchid"])).iif(!empty($_REQUEST["ticketlabelid"]), "&ticketlabelid=".intval($_REQUEST["ticketlabelid"]))."&maconfirm=1");
		exit;
	} else if (!empty($_POST["s_query"])) {
		// We have a search to do...
		require_once ("./modules/tickets/functions_ticketsearch.php");

		$noresult = "";
		if ($_POST["searchfield"] == "ticketid")
		{
			$_result = searchTicketID($_POST["s_query"]);
			if ($_result["type"] == SEARCH_TICKETID)
			{
				header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_result["ticketid"]));
				exit;
			} else if ($_result["type"] == SEARCH_TICKETIDLIST) {
				$_ticketsearchid = $_result["ticketsearchid"];
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorticketid"], htmlspecialchars($_POST["s_query"]));
			}
		} else if ($_POST["searchfield"] == "fullname") {
			$_result = searchFullName($_POST["s_query"]);
			if ($_result)
			{
				$_ticketsearchid = $_result;
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorfullname"], htmlspecialchars($_POST["s_query"]));
			}
		} else if ($_POST["searchfield"] == "lastreplier") {
			$_result = searchLastReplier($_POST["s_query"]);
			if ($_result)
			{
				$_ticketsearchid = $_result;
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorlastreplier"], htmlspecialchars($_POST["s_query"]));
			}
		} else if ($_POST["searchfield"] == "contents") {
			$_result = doTicketSearch($_POST["s_query"]);
			if ($_result)
			{
				$_ticketsearchid = $_result;
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorquery"], htmlspecialchars($_POST["s_query"]));
			}
		} else if ($_POST["searchfield"] == "subject") {
			$_result = doTicketSearch($_POST["s_query"], true);
			if ($_result)
			{
				$_ticketsearchid = $_result;
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorquery"], htmlspecialchars($_POST["s_query"]));
			}
		}

		if ($_ticketsearchid)
		{
			header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($_ticketsearchid)."&s_query=".urlencode($_POST["s_query"])."&searchfield=".urlencode($_POST["searchfield"]));
			exit;
		}
	}

	if (!empty($_REQUEST["ticketsearchid"]))
	{
		$_SWIFT["ticketsearch"] = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."ticketsearches` WHERE `ticketsearchid` = '". intval($_REQUEST["ticketsearchid"]) ."';");
	}

	$_assigns = getAssignedDepartments(MODULE_TICKETS);
	renderTicketTree();

	$_ticketviews = getStaffTicketViews($_SWIFT["staff"]["staffid"]);
	$_ticketviewid = getDefaultTicketView($_SWIFT["staff"]["staffid"]);

	$template->assign("enticketview", count($_ticketviews));
	$template->assign("ticketviews", $_ticketviews);
	$template->assign("overlib", true);
	$template->assign("cticketviewid", intval($_ticketviewid));
	$template->assign("ticketview", $_SWIFT["ticketviewcache"][$_ticketviewid]);


	if (!empty($_ticketsearchid))
	{
		$template->assign("tticketsearchid", intval($_ticketsearchid));
	} else if (!empty($_REQUEST["ticketsearchid"])) {
		$template->assign("tticketsearchid", intval($_REQUEST["ticketsearchid"]));
		$_ticketsearchid = intval($_REQUEST["ticketsearchid"]);
	} else if (!empty($_REQUEST["ticketlabelid"])) {
		$template->assign("tticketlabelid", intval($_REQUEST["ticketlabelid"]));
	} else {
		if (!empty($_REQUEST["departmentid"]))
		{
			$template->assign("tdepartmentid", intval($_REQUEST["departmentid"]));
		}
		if (!empty($_REQUEST["ticketstatusid"]))
		{
			$template->assign("tticketstatusid", intval($_REQUEST["ticketstatusid"]));
		}
	}

	$_autorefresh = $_SWIFT["ticketgridcache"][$_SWIFT["staff"]["staffid"]]["autorefresh"];
	if (intval($_autorefresh) > 0)
	{
		$template->assign("refreshtime", intval($_autorefresh));
	}

	if (!empty($_REQUEST["departmentid"]) && !empty($_REQUEST["ticketstatusid"]))
	{
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'" title="'.addslashes($_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"]).'">'.$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'&ticketstatusid='. intval($_REQUEST["ticketstatusid"]) .'" title="'.addslashes($_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"]).'">'.$_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"] ." &gt; ".$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"] ." &gt; ".$_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"];
	} else if (!empty($_REQUEST["departmentid"]) && empty($_REQUEST["ticketstatusid"])) {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'" title="'.addslashes($_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"]).'">'.$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"].'</a> ';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"] ." &gt; ".$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"];
	} else if (!empty($_REQUEST["ticketlabelid"])) {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&ticketlabelid='. intval($_REQUEST["ticketlabelid"]) .'">'.htmlspecialchars($_SWIFT["ticketlabelcache"][$_REQUEST["ticketlabelid"]]["title"]).'</a> ';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"] ." &gt; ".htmlspecialchars($_SWIFT["ticketlabelcache"][$_REQUEST["ticketlabelid"]]["title"]);
	} else if (!empty($_REQUEST["ticketsearchid"]) && empty($_SWIFT["ticketsearch"]["ticketfilterid"])) {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&ticketsearchid='. intval($_REQUEST["ticketsearchid"]) .'" title="'.addslashes($_SWIFT["language"]["searchquery"]).'">'.$_SWIFT["language"]["searchquery"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["searchquery"];
	} else if (!empty($_REQUEST["ticketsearchid"]) && !empty($_SWIFT["ticketsearch"]["ticketfilterid"])) {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&ticketsearchid='. intval($_REQUEST["ticketsearchid"]) .'" title="'.addslashes($_SWIFT["ticketfiltercache"][$_SWIFT["ticketsearch"]["ticketfilterid"]]["title"]).'">'.htmlspecialchars($_SWIFT["ticketfiltercache"][$_SWIFT["ticketsearch"]["ticketfilterid"]]["title"]).'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["searchquery"];
	} else {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"];
	}

	$interface->staffHeader($pagetitle, 2);

	$interface->staffNavBar($nav, "", 2);

	$_ticketstatusid = $_departmentid = array();

	if (!empty($_REQUEST["ticketstatusid"]))
	{
		$_ticketstatusid[] = intval($_REQUEST["ticketstatusid"]);
	} else {
		$_ticketstatusid = getDisplayTicketStatusList();
	}

	if (!empty($_REQUEST["ticketlabelid"]))
	{
		$_ticketlabelid = intval($_REQUEST["ticketlabelid"]);
	}

	if (!empty($_GET["newview"]))
	{
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["viewinsertconfirm"], htmlspecialchars($_GET["newview"]));
	} else if (!empty($_GET["editview"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["vieweditconfirm"], htmlspecialchars($_GET["editview"]));
	} else if (!empty($_GET["delview"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["viewdelconfirm"], htmlspecialchars($_GET["delview"]));
	} else if (!empty($_GET["maconfirm"])) {
		$_SWIFT["infomessage"] = $_SWIFT["language"]["massactionconfirm"];
	} else if (!empty($_GET["delconfirm"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["delconfirm"], htmlspecialchars($_GET["delconfirm"]));
	} else if (!empty($_GET["banconfirm"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["banconfirm"], htmlspecialchars($_GET["banconfirm"]));
	} else if (!empty($_GET["bandconfirm"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["bandconfirm"], htmlspecialchars($_GET["bandconfirm"]));
	} else if (!empty($_GET["searchticketid"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["serrorticketid"], htmlspecialchars($_GET["searchticketid"]));
	} else if (!empty($_GET["searchquery"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["serrorquery"], htmlspecialchars($_GET["searchquery"]));
	} else if (!empty($_GET["ldconfirm"])) {
		$_SWIFT["infomessage"] = $_SWIFT["language"]["ticketlabeldelconfirm"];
	} else if (!empty($_GET["invalidticket"])) {
		$_SWIFT["errormessage"] = $_SWIFT["language"]["invalidticket"];
	} else if (!empty($_GET["fnomatch"])){
		$_SWIFT["infomessage"] = $_SWIFT["language"]["nofilterticket"];
	}

	if (!empty($_REQUEST["departmentid"]) && in_array($_REQUEST["departmentid"], $_assigns))
	{
		$_departmentid[] = $_REQUEST["departmentid"];
	} else {
		$_departmentid = $_assigns;
	}

	if ($_SWIFT["staff"]["tmanage"] == "0")
	{
		$_SWIFT["errormessage"] = $_SWIFT["language"]["nopermission"];
	}
	if (isset($_GET["followupresult"]) && $_GET["followupresult"] == 0)
	{
		$_SWIFT["errormessage"] = $_SWIFT["language"]["followupfail"];
	} else if ($_GET["followupresult"] == 1) {
		$_SWIFT["infomessage"] = $_SWIFT["language"]["followupsuccess"];
	}

	?><script language="Javascript">var isTicketList = true;</script><?

	printInfoBox($_SWIFT["infomessage"]);
	printErrorBox($_SWIFT["errormessage"]);

	if ($noresult)
	{
		printTableHeader(array(), "100%", "0", "0");
		echo '<tbody><tr class="row2"><td><table border="0" cellpadding="3" cellspacing="1" width="100%">'.SWIFT_CRLF;
		echo '<tr><td width="16" align="left" valign="middle"><img src="'. $_SWIFT["themepath"] .'icon_search.gif" border="0" /></td>';
		echo '<td align="left"><span class="smalltext">'. $noresult .'</span></td>';
		echo '</tr>';
		echo '</table></td></tr></tbody></table><BR />'.SWIFT_CRLF;
		unset($_ticketsearchid);
	}

	if ($_SWIFT["staff"]["tmanage"] != "0")
	{
		renderTicketGrid($_ticketviewid, $_departmentid, $_ticketstatusid, $_ticketsearchid, $_ticketlabelid);
	}

	$interface->staffFooter();

	$seconds = fetchmicrotime()-$_SWIFT["starttime"];

}
?>